//
//  GetDomainInfo.m
//  YinHang
//
//  Created by 张亚哲 on 14/10/31.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "GetDomainInfo.h"

@implementation GetDomainInfo{
    ASIFormDataRequest *getRemainRequest;
    ASIFormDataRequest *getPastRequest;
    ASIFormDataRequest *getMoneyRequest;
    
    ASIFormDataRequest *getData;//获取续费数据
}

-(id)initWithDomainId:(NSString *)Id{
    self = [super init];
    if (self) {
        _Id = Id;
    }
    return self;
}
-(void)getInfo{
    //[self getRemain];
    //[self getPast];
    [self getMoney];
    [self getData];//获取数据
}
//获取数据
-(void)getData{
    if (![Constants isExistenceNetwork]) {
        
    } else {
        
        getData = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        getData.requestMethod = @"POST";
        // 设置超时时间
        getData.timeOutSeconds = 60;
        getData.delegate = self;
        // 设置参数
        NSString *action = @"getye";
        
        NSDictionary  *paramsDict = @{@"id":_Id,@"uid":UNAME};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        getData.postBody = [paramsData mutableCopy];
        
        [getData startAsynchronous];
    }
}
//获取余额
-(void)getRemain{
    if (![Constants isExistenceNetwork]) {
        
    } else {
        
        getRemainRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        getRemainRequest.requestMethod = @"POST";
        // 设置超时时间
        getRemainRequest.timeOutSeconds = 60;
        getRemainRequest.delegate = self;
        // 设置参数
        NSString *action = @"getycjifen";
        
        NSDictionary  *paramsDict = @{@"uid":UNAME};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        getRemainRequest.postBody = [paramsData mutableCopy];
        
        [getRemainRequest startAsynchronous];
    }
}
//获取过期时间
-(void)getPast{
    if (![Constants isExistenceNetwork]) {
        
    } else {
        
        getPastRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        getPastRequest.requestMethod = @"POST";
        // 设置超时时间
        getPastRequest.timeOutSeconds = 60;
        getPastRequest.delegate = self;
        // 设置参数
        NSString *action = @"getguoqishijian";
        
        NSDictionary  *paramsDict = @{@"id":_Id};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        getPastRequest.postBody = [paramsData mutableCopy];
        
        [getPastRequest startAsynchronous];
    }
}
//获取年限价格列表
-(void)getMoney{
    if (![Constants isExistenceNetwork]) {
        
    } else {
        
        getMoneyRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        getMoneyRequest.requestMethod = @"POST";
        // 设置超时时间
        getMoneyRequest.timeOutSeconds = 60;
        getMoneyRequest.delegate = self;
        // 设置参数
        NSString *action = @"getmoney";
        
        NSDictionary  *paramsDict = @{@"id":_Id};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        getMoneyRequest.postBody = [paramsData mutableCopy];
        
        [getMoneyRequest startAsynchronous];
    }
}

#pragma mark - ASIHttpRequestDelegate method

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@",request.responseString);
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request.responseString dataForUTF8] options:NSJSONReadingAllowFragments error:&err];
    
    // 1. 是否解析有错误
    if (!err) {
        
        // 2. 判断错误码
        NSInteger errorCode = [result[@"errorCode"]integerValue];
        if (errorCode == 0) {
            // 2.1 获取"info"
            NSDictionary *info = result[@"info"];
            NSLog(@"%@",info);
           if(request == getMoneyRequest){
                _moneyList = result[@"info"];
            }else{
                _InfoDic = result[@"info"];
            }
            [_delegate didReceiveData];
        }
        
    }else {
        [SVProgressHUD showErrorWithStatus:@"数据解析出错！"];
        [_delegate didReceiveData];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    [SVProgressHUD showErrorWithStatus:@"服务器无法访问，请稍后再试!"];
    [_delegate didReceiveData];
}

// 取消网络访问
- (void)cancelRequest {
    [getMoneyRequest cancel];
    [getPastRequest cancel];
    [getRemainRequest cancel];
}

-(void)dealloc{
    [self cancelRequest];
}

@end
